package test;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class StartPaAction extends HttpServlet{

	  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException
      
  {
		  
		  String flag= request.getParameter("flag");  
		  String url= request.getParameter("url");
		  String cookie= request.getParameter("cookie");
		  Crawler c = new MyCrawler();
			if("1".equals(flag)){
				c.initCrawlerWithSeeds(new String[] { url });
				try {	
					c.mainThreadStart(c.getParam1(), c.getParam2(),cookie);
					
					RequestDispatcher rd = request
					.getRequestDispatcher("/sucess.jsp");
			         rd.forward(request, response);
			         return;
					
				} catch (Exception e) {
					RequestDispatcher rd = request
					.getRequestDispatcher("/error.jsp");
			         rd.forward(request, response);
			         return;
				}
			}
			else{
				try {
				DownLoadFile downfile= new DownLoadFile();	
				List list = (List)downfile.getDirectoryFromHdfs();
				request.setAttribute("list", list); 
				RequestDispatcher rd = request
				.getRequestDispatcher("/querytitle.jsp");
		         rd.forward(request, response);

		         return ;
				}catch (Exception e) {
					RequestDispatcher rd = request
					.getRequestDispatcher("/error.jsp");
			         rd.forward(request, response);
			         return;
				}
			
			}
  }


}
